﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Framework.Util;


namespace WebSite.Company
{
    public partial class CompanyEdit : System.Web.UI.Page
    {
        private string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";
            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            //判断该公司是否为该用户的
            Users uinfo = UserBase.GetCurrentUser();
            CompanysHelper chelper = new CompanysHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "ID", id);
            c.Add(CriteriaType.Equals, "OwnerUserID", uinfo.ID);
            c.Add(CriteriaType.Equals, "Flag", 0);
            List<Companys> clist = chelper.QueryCompanysBySearch(c, null);
            if (clist != null && clist.Count > 0)
            {
                string companyname = clist[0].CompanyName;
                string brief = clist[0].Brief;
                txtCompanyName.Text = companyname;
                txtBrief.Text = brief;
                string realmname = clist[0].RealmName;
                litRealmName.Text = "http://www.youbewin.com/c/"+realmname+".aspx";
                int type = clist[0].Type;
                if (type == 1)
                {
                    rbCheck.Checked = true;
                }
                else if (type == 2)
                {
                    rbNotCheck.Checked = true;
                }

                string smallheadimage = clist[0].SmallHeadImage;
                if (string.IsNullOrEmpty(smallheadimage))
                {
                    imgHead.ImageUrl = "../images/xiaozuphoto.jpg";
                }
                else
                {
                    imgHead.ImageUrl = smallheadimage;
                }
            }
            else
            {
                Tools.AlertAndRedirect("该小组不存在或没有权限进行更改", "CompanyList.aspx");
                return;
            }
        }
        #endregion

        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string companyname = txtCompanyName.Text.Trim();
            string brief = txtBrief.Text.Trim();
            string changeusername = txtChangeUserName.Text.Trim();
            int type = 1;
            if (rbCheck.Checked)
            {
                type = 1;
            }
            else if (rbNotCheck.Checked)
            {
                type = 2;
            }
            Companys cinfo = new Companys();
            CompanysHelper chelper = new CompanysHelper();
            cinfo = chelper.GetCompanys(id);
            cinfo.CompanyName = companyname;
            cinfo.Brief = brief;
            cinfo.Type = type;

            string fileFullName = "";
            string newfilename = "";
            string filePath = "";
            string fileFolder = "/Files/headphoto/";

            string originalImagePath = "";
            string thumbnailPath = "";

            if (fuFile.HasFile)
            {
                //判断文件的大小
                string FileName = fuFile.FileName.ToString();
                string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                if (ExtenName.ToLower() == ".jpg" || ExtenName.ToLower() == ".png" || ExtenName.ToLower() == ".bmp" || ExtenName.ToLower() == ".jpeg" || ExtenName.ToLower() == ".gif")
                {
                    string serverPath = "..\\Files\\headphoto\\";
                    filePath = Server.MapPath(serverPath);
                    string newfilepath = filePath;

                    if (!System.IO.Directory.Exists(filePath))
                    {
                        System.IO.Directory.CreateDirectory(filePath);
                    }

                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss");

                    fileFullName = filename + ExtenName;
                    newfilename = filename + "new" + ExtenName;
                    filePath += fileFullName;
                    newfilepath += newfilename;
                    fuFile.SaveAs(filePath);

                    //压缩图片
                    originalImagePath = filePath;
                    thumbnailPath = newfilepath;

                    int width = 150;
                    int height = 150;
                    string mode = "Cut";
                    string imageType = "";
                    if (ExtenName.ToLower() == ".gif")
                    {
                        imageType = ".gif";
                    }
                    else
                    {
                        imageType = ".jpg";
                    }
                    ImageUtils.MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode, imageType);
                    //将原图片删除
                    System.IO.File.Delete(filePath);
                }
                else
                {
                    Tools.Alert("图片格式不正确");
                    return;
                }
            }
            if (!string.IsNullOrEmpty(thumbnailPath))
            {
                cinfo.SmallHeadImage = fileFolder + newfilename;
            }

            if (!string.IsNullOrEmpty(changeusername))
            {
                //用户名不能和当前用户名相同
                Users uinfo = UserBase.GetCurrentUser();
                if (changeusername.ToLower() == uinfo.UserName.ToLower())
                {
                    Tools.Alert("更改所有人不能为自己");
                    return;
                }
                //判断用户名是否存在
                UsersHelper uhelper = new UsersHelper();
                Users changuinfo = uhelper.GetUserInfoByUserName(changeusername);
                if (changuinfo == null)
                {
                    Tools.Alert("更改所有人用户名不存在，请重新输入");
                    return;
                }
                else
                {
                    string changeuserid = changuinfo.ID;
                    cinfo.OwnerUserID = changeuserid;
                    cinfo.OwnerNickName = changuinfo.NickName;
                }
            }
            chelper.EditCompanys(cinfo);

            Tools.AlertAndRedirect("修改小组信息成功", "CompanyList.aspx");
            return;
        }
        #endregion

        #region 返回
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("CompanyList.aspx");
        }
        #endregion
    }
}